package com.loongtech.bi.action.game;

import com.loongtech.bi.entity.game.EntityNewUserCountry;
import com.loongtech.bi.manager.game.NewUserCountryManager;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.CommonGameParam;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * 游戏内分析-国家分析Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("view")
public class CountryGameAction {
	private Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - 8 * Time.DAY));  // 前端传过来的开始时间
	private Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY));  // 前端传过来的结束时间
	
	private List<NewUserCountryManager.CountryGameData> countryGameData;
	
	@Autowired
	NewUserCountryManager newUserCountryManager;
	
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;
	
	@PostConstruct
	public void init() {
		search();
	}

	public void search() {
		sysLogManager.addLog(session.getUserName(), "游戏内分析-国家分析");

        countryGameData = new ArrayList<>();
		
		List<EntityNewUserCountry> newUserCountryList = newUserCountryManager.getAllNewUserCountry(dateStart, dateEnd);

        if(newUserCountryList != null && newUserCountryList.size() > 0) {
            Map<Integer, NewUserCountryManager.CountryGameData> map = new LinkedHashMap<>();
            int allNewRole = 0;
            for (EntityNewUserCountry e : newUserCountryList) {
                allNewRole += e.getUser_number();

                int cid = e.getCountryid();
                int oid = e.getOccupationid();
                NewUserCountryManager.CountryGameData c = map.get(cid);
                if(c == null) {
                    c = newUserCountryManager.new CountryGameData();
                    map.put(cid, c);
                }

                c.setCountryid(cid);
                c.setCountryNewRole(c.getCountryNewRole() + e.getUser_number());
                Map<Integer, Integer> l = c.getNewRoleSchoolCounts();
                if(l == null) {
                    l = new LinkedHashMap<>();
                    c.setNewRoleSchoolCounts(l);
                }
                l.put(oid, (l.get(oid) == null ? 0 : l.get(oid)) + e.getUser_number());
            }

            for (Map.Entry<Integer, NewUserCountryManager.CountryGameData> entry : map.entrySet()) {
                entry.getValue().setAllNewRole(allNewRole);
                countryGameData.add(entry.getValue());
            }
        }
	}
	
	public String getSchoolNameById(int id) {
		return GameParamConfigManager.getItemsRevise(CommonGameParam.SCHOOL).get(String.valueOf(id)) == null ? id + "" : GameParamConfigManager.getItemsRevise(CommonGameParam.SCHOOL).get(String.valueOf(id)); //SchoolManager.getSchoolNameById(id);
	}

    public String getCountryNameById(int id) {
        return GameParamConfigManager.getItemsRevise(CommonGameParam.NATION_NAME).get(String.valueOf(id)) == null ? id + "" : GameParamConfigManager.getItemsRevise(CommonGameParam.NATION_NAME).get(String.valueOf(id)); //SchoolManager.getSchoolNameById(id);
    }
	
	public Date getDateStart() {
		return dateStart;
	}

	public void setDateStart(Date dateStart) {
		this.dateStart = dateStart;
	}

	public Date getDateEnd() {
		return dateEnd;
	}

	public void setDateEnd(Date dateEnd) {
		this.dateEnd = dateEnd;
	}

    public List<NewUserCountryManager.CountryGameData> getCountryGameData() {
        return countryGameData;
    }

    public void setCountryGameData(List<NewUserCountryManager.CountryGameData> countryGameData) {
        this.countryGameData = countryGameData;
    }
}
